<!--
 * @Description: 设备管理
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: Ronda
 * @LastEditTime: 2022-05-23 15:02:33
-->
<template>
  <div>
    <!-- 搜索按钮下拉框和录入 -->
    <div class="search">
      <el-row>
        <el-col :span="20">
          <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form-item>
              <el-select
                clearable
                v-model="form.online_status"
                placeholder="请选择设备状态"
              >
                <el-option label="在线" value="1"></el-option>
                <el-option label="离线" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select
                clearable
                v-model="form.bind_status"
                placeholder="请选择设备绑定状态"
              >
                <el-option label="已绑定" value="1"></el-option>
                <el-option label="未绑定" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input
                v-model="form.name"
                placeholder="请输入设备名称"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSearch" icon="el-icon-search"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="4" style="text-align: right">
          <el-button type="primary" @click="toAdd">录入</el-button>
        </el-col>
      </el-row>
    </div>
    <!-- 表格 -->
    <div class="table">
      <el-table :data="empData" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="serial_number" label="编号" width="180">
        </el-table-column>
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column prop="online_status" label="在线状态" align="center">
          <template v-slot="scope">
            <div>
              <el-tag type="success" v-if="scope.row.online_status === 1"
                >在线</el-tag
              >
              <el-tag type="danger" v-else>离线</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="bind_status" label="绑定状态" align="center">
          <template v-slot="scope">
            <div>
              <el-tag type="success" v-if="scope.row.bind_status === 1"
                >已绑定</el-tag
              >
              <el-tag type="warning" v-else>未绑定</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div>
              <el-button type="text" @click="toEdit(scope.row)">修改</el-button>
              <el-button type="text" @click="toDelete(scope.row.id)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页按钮 -->
    <div class="page" style="margin-top: 15px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="text-align: left"
        hide-on-single-page
      >
      </el-pagination>
    </div>
    <!-- 抽屉 -->
    <el-drawer
      @close="toClose"
      size="50%"
      :title="title"
      :visible.sync="dialog"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
    >
      <div class="demo-drawer__content">
        <!-- 表单 -->
        <el-form :model="form2" :rules="rules" ref="myForm">
          <el-row>
            <el-col :span="24">
              <el-form-item
                label="编号"
                prop="serial_number"
                :label-width="formLabelWidth"
              >
                <el-input
                  v-model="form2.serial_number"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item
                label="名称"
                prop="name"
                :label-width="formLabelWidth"
              >
                <el-input v-model="form2.name" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="dialog = false">取 消</el-button>
          <el-button type="primary" @click="toSave">确定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { get, postJSON, del } from "@/utils/request";
export default {
  data() {
    return {
      // 表单对象
      form: {},
      // 录入表单数据
      form2: {},
      // 总页数
      total: 0,
      // 表单标题
      title: "",
      empData: [],
      params: {
        page: 1,
        pageSize: 5,
      },
      // 控制抽屉弹出
      dialog: false,
      // 表单文字宽度
      formLabelWidth: "80px",
      // 校验规则
      rules: {
        serial_number: [
          { required: true, message: "请输入设备编号", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入设备名称", trigger: "blur" }],
      },
    };
  },
  computed: {},
  methods: {
    // 数据条数发生变化的处理函数
    handleSizeChange(pageSize) {
      // console.log(pageSize,'条数');
      this.params.pageSize = pageSize;
      this.getEmpData();
    },
    // 页码发生改变的函数
    handleCurrentChange(page) {
      // console.log(page,'当前页码');
      this.params.page = page;
      this.getEmpData();
    },
    // 去删除
    toDelete(id) {
      this.$confirm("此操作将删除该设备, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 发送删除请求
          let res = await del("/device/deleteById", {
            id: id,
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 将页码改为1
          this.params.page = 1;
          // 重新请求工程数据
          this.getEmpData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 关闭模态框，清空表单校验
    toClose() {
      this.$refs["myForm"].resetFields();
      this.dialog = false;
    },
    // 去修改
    toEdit(row) {
      // 清空
      this.form2={};
      this.dialog = true;
      this.title = "修改工程信息";
      // 深拷贝
      this.form2 = { ...row }; //
    },
    // 去提交
    toSave() {
      this.$refs["myForm"].validate(async (valid) => {
        // 校验通过
        if (valid) {
          // alert('submit!');
          // console.log(this.form);
          // 深拷贝
          let obj = { ...this.form2 };
          // console.log(obj);
          // 发送更新请求
          let res = await postJSON("/device/saveOrUpdate", obj);
          if (res.status === 200) {
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
            // 清空表单校验
            this.toClose();
            this.getEmpData();
          } else {
            this.$notify({
              title: "失败",
              message: res.message,
              type: "error",
            });
          }
          // 关闭抽屉
          this.dialog = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 去录入
    toAdd() {
      // 清空
      this.form2={};
      // 弹出抽屉
      this.dialog = true;
      // 修改标题
      this.title = "录入工程信息";
      // 去清空
      this.form = {};
    },
    async getEmpData() {
      // 同时处理搜索和分页查询
      let tem = { ...this.form, ...this.params };
      // 空属性不传递给后台 删除空属性
      for (let key in tem) {
        if (!tem[key]) {
          delete tem[key];
        }
      }
      // console.log(tem, 'tem');
      let res = await get("/device/pageQuery", tem);
      // console.log(res);
      this.empData = res.data.list;
      this.total = res.data.total;
    },
    // 搜索请求
    onSearch() {
      // 页码从1开始搜索
      this.params.page = 1;
      this.getEmpData();
    },
  },
  created() {
    this.getEmpData();
  },
  mounted() {},
};
</script>
<style scoped>
</style>
